KEMBAR78
Encapsulation | PDF | Programming | Constructor (Object Oriented Programming)
0% found this document useful (0 votes)
11 views8 pages

Encapsulation

The document contains multiple Java code snippets demonstrating the concepts of encapsulation, inheritance, interfaces, and abstraction. Each section includes class definitions and methods that illustrate how these object-oriented programming principles are applied. The main method in each example shows how to create objects and invoke their methods.

Uploaded by

suzana9225
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

Encapsulation

The document contains multiple Java code snippets demonstrating the concepts of encapsulation, inheritance, interfaces, and abstraction. Each section includes class definitions and methods that illustrate how these object-oriented programming principles are applied. The main method in each example shows how to create objects and invoke their methods.

Uploaded by

suzana9225
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Encapsulation:

class Animal2 {
String name;

public Animal2(String name) {


this.name = name;
}

public void Sound() {


System.out.println(name + " is making a sound");
}
}
class cat1 extends Animal2{
public cat1(String name) {
super(name);
}
@Override
public void Sound(){
System.out.println(name + " is meowing");
}
public void fetching(){
System.out.println(name + " is fetching the ball");
}
}
public class encapsulation {
public static void main(String[] args) {
Animal2 dog = new Animal2("Husky");
dog.Sound();
cat1 c = new cat1("calico");
c.Sound();
c.fetching();
}
}

Output:
Inheritance:
class base{
private int x;
public int getX(){
return x;
}
public void setX(int x){
System.out.println("Setting x as ");
this.x= x;
}
public void printMe(){
System.out.println("an example of constructor");
}
}
class derived extends base{
private int y;
public int getY(){
return y;
}
public void setY(int y){
this.y= y;
}
public void sqrt(){
System.out.println("the square = " + (y *y));
}
}
class Animal1{
String name;
public Animal1(String name){
this.name =name;
}
public void sound(){
System.out.println(name + " is making sound");
}
}
class Dog1 extends Animal1{
public Dog1(String name){
super(name);
}
@Override
public void sound() {
System.out.println(name + " is barking");
}
public void fetch(){
System.out.println(name + " is fetching a ball");
}
}
public class inheritence {
public static void main(String[] args) {
Animal1 cat = new Animal1("Persian");
cat.sound();
Dog1 dog = new Dog1("Golden Retriever");
dog.sound();
dog.fetch();
base b = new base();
b.setX(98);
System.out.println(b.getX());
b.printMe();
derived d = new derived();
d.setX(78);
System.out.println(d.getX());
d.setY(45);
System.out.println(d.getY());
d.printMe();
d.sqrt();
}
}

Output:

Interface:
interface Bicycle{
int a = 45;
void applyBrake(int decrement);
void speedup(int increment);
}
interface Horncycle{
void applyhorn();
void horn();
}
class Avoncycle implements Bicycle, Horncycle{
int speed =7;
public void applyBrake(int decrement)
{
speed = speed - decrement;
System.out.println(speed);
}
public void speedup(int increment){
speed = speed + increment;
System.out.println(speed);
}
public void applyhorn(){
System.out.println("press horn");
}
public void horn(){
System.out.println("stop it");
}
public void newn(){
System.out.println("for testing");
}
}
public class interfaces {
public static void main(String[] args) {
Avoncycle ch = new Avoncycle();
ch.applyBrake(3);
ch.speedup(4);
System.out.println(ch.a);
ch.applyhorn();
ch.horn();
ch.newn();
}
}

Output:
Abstraction:
abstract class parent{
public parent(boolean name){
if(name)
System.out.println("This is a constructor");
}
public void sayHello(){
System.out.println("Hello");
}
abstract public void greet();
abstract public void shake();
}
class child extends parent{
// to avoid printing the constructor we need to create a constructor of extended
class
// and use super() to if needed
public child(){
super(false);
}
@Override
public void greet() {
System.out.println("Greeting");
}
public void shake() {
System.out.println("Shaking");
}
}
//to extend a class we must do
// 1. make the extended class an abstract class or
// 2. override the abstract method of the abstract base class
// class child2 extends parent{
// public void nod(){
// System.out.println("this will show an errer");
abstract class child2 extends parent{
public child2(){
super(true);
}
public void nod(){
System.out.println("Nodding");
}
abstract public void greet2();
abstract public void greet3();
}
public class abstraction {
public static void main(String[] args) {
// parent p = new parent() ; error
// child2 ch = new child2(); error
// we can't create an object of a abstract class
child c = new child();
c.greet();
c.shake();
}
}

Output:

You might also like