Core Java(Part-6)

Content
.OOPs(Object Oriented Programming Language)
.Inheritance
.Encapsulation
.Polymorphism
.Abstraction
.Constructor
What is OOPs ?
Object-oriented programming(OOP) is a computer model that organize the code into objects rather the functions.
What is class?
Class is a collection of objects, class don’t consume any space in the memory.
What is object?
Object is a real world entity which have properties and function.
Object is also called as instance of class
Object take some space in memory
Java is all about objects, and objects are like real-life things:
A Car has wheels and can drive.
A Dog has a name and can bark.
OOP says: “Hey, write code like how the world works — with objects.”
Inheritance
Inheritance=”I got it from my parents”
“Child class gets stuff from parents class-no need to rewrite.“
“Child copying things from parents“
In Inheritance, it has two class one is Parent class and another one is Child class. Child class inherit the properties of Parent class. Whatever the properties have Parent class that all properties Child class also have due to inheritance, by using extends keyword you can inherit all the properties and behavior of Parent class.
class Animal {
void eat() {
System.out.println("nom nom");
}
}
class Dog extends Animal {
void bark() {
System.out.println("woof");
}
}
Types of Inheritance
.Single level
.Multi level
.Hierarchical
.Hybrid
.Multiple
Type | Explanation | Example |
Single Inheritance | One child, one parent. | Son → Father |
Multilevel Inheritance | Grandchild copying from Child, who copied from Parent. | Grandson → Son → Father |
Hierarchical Inheritance | Multiple children copying from one parent. | Son & Daughter → Father |
Multiple Inheritance (by interface) | Child copying from many parents (but only allowed using interface ). | Son copying from Father + Teacher |
Hybrid Inheritance | Mixing different types (Single + Multiple + Multi-level). (Java allows only via interfaces.) | Complicated Family Tree |
1. Single Inheritance
class Animal {
void eat() {
System.out.println("eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("barking...");
}
}
Dog inherits eat() from Animal.
2. Multilevel Inheritance
class Animal {
void eat() {
System.out.println("eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("barking...");
}
}
class Puppy extends Dog {
void weep() {
System.out.println("weeping...");
}
}
Puppy inherits from Dog → Dog inherits from Animal → So Puppy gets both bark() and eat().
3. Hierarchical Inheritance
class Animal {
void eat() {
System.out.println("eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("barking...");
}
}
class Cat extends Animal {
void meow() {
System.out.println("meowing...");
}
}
Dog and Cat both inherit from Animal.
4. Multiple Inheritance (via Interface)
interface Father {
void height();
}
interface Mother {
void beauty();
}
class Child implements Father, Mother {
public void height() {
System.out.println("Tall");
}
public void beauty() {
System.out.println("Beautiful");
}
}
Child copies from both Father and Mother using interface. (Java doesn't allow class-class multiple inheritance to avoid confusion.)
Important points
Java does not support multiple inheritance with classes directly because of Diamond Problem (confusion).
But Java supports multiple inheritance through interfaces!
In Inheritance it has 3 type of methods
.inherited method
.overridden method
.specialised method
inherited method
It means that you just inherit it from your Parent class and use that property in your child class without any changes.
class Parent{
public void eat(){
System.out.println(“Parent eat“);
}
}
class Child extends Parent{
}
public class InheritedInheritance{
public static void main(String args[]){
Child ch=new Child();
ch.eat
}
}
Overridden method
It means you inherit it from your parent and after inherit you make some changes in the properties
class Parent{
public void eat(){
System.out.println(“Parent eat“);
}
}
public class Child extends Parent{
public void eat(){
System.out.println(“Child loves toys”);
}
}
public class OverriddenInheritance{
public static void main(String args[]){
Child ch= new Child();
ch.eat();
}
}
Specialised method
It means child have some special properties and behavior that Parent didn’t have it is knows specialised method. You can’t directly access through Parent reference variable you have to downcast it to access child properties through Parent reference eg:- (Child)par).smoke();
class Parent{
public void eat(){
System.out.println("Parent eat");
}
}
public class Child extends Parent{
public void eat(){
System.out.println("Child loves toys");
}
public void smoke(){
System.out.println("child smoke");
}
}
public class OverriddenInheritance{
public static void main(String args[]){
Parent par= new Child();
((Child)par).smoke();//downcast
}
}
Data hiding: Process of hiding data from outside world.
Encapsulation
In lame term “you can’t access or touch my data directly ask nicely”
In encapsulation the data is hidden for the other class you can’t directly access it.
How Encapsulation is perform?
We make the variable private means( we use private keyword with datatype and variable name to perform encapsulation) when we write private with datatype and variable name then we can only within the class but not from outside the class you cannot access that variable by creating the object also. To access that variable from different class we create setter and getter method in that class. In setter method we pass the parameter (value) of private variable and through getter method you can return the value of that private variable
class BankAccount {
private int balance = 1000;
public void deposit(int amount) {
balance += amount;
}
public int getBalance()//Getter method {
return balance;
}
}
balance
is private — you can’t mess with it unless the class lets you.
Polymorphism
“Same word different meaning“
“Everyone can say talk() but they talk their own way“
class Human {
void speak() {
System.out.println("Hello");
}
}
class Baby extends Human {
void speak() {
System.out.println("Goo goo");
}
}
“A baby and an adult both speak(), but not the same way
Polymorphism is of two types
1. Compile time Polymorphism(Method Overloading)
In method overloading we have multiple method with same name but all method have different parameter is known as Method Overloading
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
Here add()
is used in two ways — 2 numbers or 3 numbers. Decided during compile time
2.Run time Polymorphism(Method Overriding)
Method Overloading is a concept of inheritance in Method Overriding Child class inherit the properties(method) of Parent class by using extends keyword. So in the below program you see both the class have same method due to inheritance. Dog class inherit that method form the Animal class, so after inherit Dog class do some modification or updating in the sound() method that is known as Method Overriding
class Animal {
void sound() {
System.out.println("Some sound...");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Bark");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Meow");
}
}
At runtime, Java decides:
If Dog → sound()
→ Bark
If Cat → sound()
→ Meow
private, final static method can’t be overriding
Abstraction
“Use it without knowing the messy stuff thing inside“
When you drive a car:
You press the accelerator, the car moves .
You don't care how the engine, fuel injection, wiring, etc. work.
Car hides all complex parts and shows only steering, pedals, gear = abstraction!
In Java
Abstract Class or Interface = used for abstraction.
They show only the important functions, hiding the full details.
abstract class Remote {
abstract void pressPower();
}
class TVRemote extends Remote {
void pressPower() {
System.out.println("TV is now ON");
}
}
You just press power — you don’t care how electricity works.
OOP Thing | Lazy Meaning |
Encapsulation | Don’t touch my stuff |
Inheritance | I got this from my parent |
Polymorphism | Same name, new trick |
Abstraction | Just use it, bro |
Using Interface:
interface Vehicle { void start(); // only method signature } class Car implements Vehicle { public void start() { System.out.println("Car started!"); } }
Important Points
Abstract class = can have both abstract methods and normal methods.
Interface = only abstract methods (before Java 8) — now can have default methods too.
You cannot create object of abstract class directly.
Constructor
Constructor name is same as class name
When you create an object then by it class default constructor(zero parameterized constructor)
public class EncapsulationDemo {
public static void main(String args[]){
char arr[]={'a', 'b', 'c', 'd'};
String s=new String(arr);
System.out.println(s);
}
}//output abcd
You can write constructor in two different ways
.Default constructor(zero parameterized constructor)
class Animal{
Animal(){
System.out.println(“Default Constructor”);
}
public void hello(){
System.out.println(“hello“);
}
public class Tiger{
public static void main(String args[]){
Animal animal= new Animal();
animal.hello();
}
}
.Parameterized Constructor
This constructor calls only when you pass the parameter in the object
class Animal{
Animal(){
System.out.println(“Default Constructor”);
}
Animal(String s){
this.a=s;
System.out.println(a+"Parametrized Constructor");
}
public void hello(){
System.out.println("Default constructor");
}
}
public class Tiger{
public static void main(String args[]){
Animal animal= new Animal();
Animal animal=new Animal(“Tiger“);
animal.hello();
}
}
this keyword
this
means "myself" (the current object).
package constructor;
class City2{
public void m1(){
System.out.println("Jaipur");
}
}
class City {
City2 city2;
public void m1(){
city2.m1();
}
City(City2 city2){
this.city2=city2;
}
}
public class DemoCity{
public static void main(String args[]){
City2 city2=new City2();
City c=new City(city2);
c.m1();
}
}
You have two classes:
City2
andCity
.City2
has a methodm1()
that prints"Jaipur"
.City
has:A reference to a
City2
object.A constructor that accepts a
City2
object and assigns it tothis.city
2
.A method
m1()
that callscity2.m1()
.
DemoCity
is the main class that runs everything.
In
main()
, you create an object:City2 city2 = new City2();
→ Now
city2
is an object ofCity2
class.Then you create another object:
City c = new City(city2);
→ You pass the
city2
object toCity
's constructor.
→ Inside the constructor,this.city
2 = city2;
connects the two objects.Now, when you call:
c.m1();
→ It internally calls
city2.m1()
. →city2.m1()
prints"Jaipur"
.
Output:
Jaipur
Visual Diagram:
City2 object (city2)
|
|
City object (c) --> holds reference to --> city2
|
|
c.m1() --> calls --> city2.m1() --> prints Jaipur
Simple explanation of this
here:
When you wrote:
this.city2 = city2;
this.city
2
→ means the City class’s own city2 field.city2
→ means the parameter you passed in the constructor.
this
clears the confusion.
Subscribe to my newsletter
Read articles from Khelendra Kumar Thakur directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
